package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexSearchVo;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;

    @GetMapping(value = "/upperAlbum/{albumId}")
    public Result upperAlbum(@PathVariable(value = "albumId") Long albumId) {
        searchService.upperAlbum(albumId) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @GetMapping(value = "/lowerAlbum/{albumId}")
    public Result lowerAlbum(@PathVariable(value = "albumId") Long albumId) {
        searchService.lowerAlbum(albumId) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    // 批量上架接口，为了测试
    @GetMapping(value = "/batchUpperAblum")
    public Result batchUpperAblum() {
        for(long x = 2 ; x <= 1500 ; x++) {
            searchService.upperAlbum(x);
        }
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    // 搜索专辑的接口
    @PostMapping
    public Result<AlbumSearchResponseVo> search(@RequestBody AlbumIndexQuery albumIndexQuery) {
        AlbumSearchResponseVo albumSearchResponseVo =  searchService.search(albumIndexQuery) ;
        return Result.build(albumSearchResponseVo , ResultCodeEnum.SUCCESS) ;
    }

    @GetMapping(value = "/channel/{category1Id}")
    public Result<List<AlbumInfoIndexSearchVo>> channel(@PathVariable(value = "category1Id") Long c1Id) {
        List<AlbumInfoIndexSearchVo> albumInfoIndexSearchVo =  searchService.channel(c1Id) ;
        return Result.build(albumInfoIndexSearchVo , ResultCodeEnum.SUCCESS) ;
    }

    @GetMapping(value = "/completeSuggest/{keyword}")
    public Result<List<String>> completeSuggest(@PathVariable(value = "keyword") String keyword) {
        List<String> list =   searchService.completeSuggest(keyword) ;
        return Result.build(list , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 更新排行榜数据
     */
    @GetMapping(value = "/updateLatelyAlbumRanking")
    public Result updateLatelyAlbumRanking() {
        log.info("SearchApiController...updateLatelyAlbumRanking...执行了...");
        searchService.updateLatelyAlbumRanking() ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @GetMapping(value = "/findRankingList/{category1Id}/{dimension}")
    public Result<List<AlbumInfoIndex>> findRankingList(@PathVariable(value = "category1Id") Long category1Id ,
                                                        @PathVariable(value = "dimension") String dimension) {
        List<AlbumInfoIndex> list =  searchService.findRankingList(category1Id , dimension) ;
        return Result.build(list , ResultCodeEnum.SUCCESS) ;
    }
}

